<template>
  <div class="bound-detail-view">
    <div v-loading="loading">
      <!-- 入库单标题和操作按钮 -->
      <div class="document-header">
        <h1 class="document-title">入库单</h1>
        <div class="header-actions">
          <el-button type="success" @click="handleBack" class="back-btn">返回</el-button>
          <el-button type="primary" icon="el-icon-printer" @click="handlePrint" class="print-btn">打印</el-button>
        </div>
      </div>

      <!-- 基本信息 -->
      <div class="basic-info">
        <div class="info-row">
          <div class="info-item">
            <span class="label">单号:</span>
            <span class="value">{{ boundDetail.boundCode || '--' }}</span>
          </div>
          <div class="info-item">
            <span class="label">操作状态:</span>
            <span class="value">{{ boundDetail.boundStatusName || '入库' }}</span>
          </div>
          <div class="info-item">
            <span class="label">制单人:</span>
            <span class="value">{{ boundDetail.creator || '--' }}</span>
          </div>
          <div class="info-item">
            <span class="label">制单日期:</span>
            <span class="value">{{ formatDate(boundDetail.creatorTime) }}</span>
          </div>
        </div>
        <div class="info-row">
          <div class="info-item">
            <span class="label">供应商:</span>
            <span class="value">{{ boundDetail.supplierName || '--' }}</span>
          </div>
          <div class="info-item">
            <span class="label">供应商联系人:</span>
            <span class="value">{{ boundDetail.supplierContactName || '--' }}</span>
          </div>
          <div class="info-item">
            <span class="label">供应商电话:</span>
            <span class="value">{{ boundDetail.supplierContactPhone || '--' }}</span>
          </div>
          <div class="info-item">
            <span class="label">仓库归属:</span>
            <span class="value">{{ boundDetail.communityName || '--' }}</span>
          </div>
        </div>
        <div class="info-row">
          <div class="info-item">
            <span class="label">入库仓库:</span>
            <span class="value">{{ boundDetail.warehouseName || '--' }}</span>
          </div>
          <div class="info-item">
            <span class="label">仓库联系人:</span>
            <span class="value">{{ boundDetail.warehouseContactName || '--' }}</span>
          </div>
          <div class="info-item">
            <span class="label">仓库电话:</span>
            <span class="value">{{ boundDetail.warehouseContactPhone || '--' }}</span>
          </div>
          <div class="info-item">
            <span class="label">仓库地址:</span>
            <span class="value">{{ boundDetail.warehouseAddress || '--' }}</span>
          </div>
        </div>
        <div class="info-row" v-if="boundDetail.boundDesc">
          <div class="info-item full-width">
            <span class="label">备注:</span>
            <span class="value">{{ boundDetail.boundDesc }}</span>
          </div>
        </div>
      </div>

      <!-- 详细入库列表表格 -->
      <div class="table-section">
        <table class="detail-table">
          <thead>
            <tr>
              <th>序号</th>
              <th>货品名称</th>
              <th>品牌</th>
              <th>规格型号</th>
              <th>单位</th>
              <th>供应商</th>
              <th>数量</th>
              <th>单价</th>
              <th>合计</th>
              <th>备注</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(item, index) in boundInfoList" :key="index">
              <td>{{ index + 1 }}</td>
              <td>{{ item.materialName || '--' }}</td>
              <td>{{ item.materialBrand || '--' }}</td>
              <td>{{ item.materialSpecifications || '--' }}</td>
              <td>{{ item.materialUnit || '--' }}</td>
              <td>{{ boundDetail.supplierName || '--' }}</td>
              <td class="number">{{ item.materialNum || 0 }}</td>
              <td class="number">{{ formatPrice(item.materialPrice) }}</td>
              <td class="number">{{ formatPrice(item.subtotal) }}</td>
              <td>{{ boundDetail.boundDesc || '--' }}</td>
            </tr>
            <!-- 空行填充 -->
            <tr v-for="n in Math.max(0, 5 - boundInfoList.length)" :key="'empty-' + n">
              <td>{{ boundInfoList.length + n }}</td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
            </tr>
          </tbody>
        </table>
      </div>

      <!-- 汇总和签名区域 -->
      <div class="summary-section">
        <div class="summary-left">
          <div class="summary-item">
            <span class="label">金额大写:</span>
            <span class="value">{{ boundDetail.totalAmountInWords || amountInWords }}</span>
          </div>
        </div>
        <div class="summary-center">
          <div class="summary-item">
            <span class="label">总价合计:</span>
            <span class="value total-amount">{{ formatPrice(boundDetail.totalAmount || totalAmount) }}</span>
          </div>
        </div>
        <div class="summary-right">
          <div class="summary-item">
            <span class="label">入库人:</span>
            <span class="value">{{ boundDetail.warehouseKeeper || boundDetail.creator || '--' }}</span>
          </div>
          <div class="summary-item">
            <span class="label">入库日期:</span>
            <span class="value">{{ formatDate(boundDetail.creatorTime) }}</span>
          </div>
          <div class="summary-item">
            <span class="label">入库仓库:</span>
            <span class="value">{{ boundDetail.warehouseName || '--' }}</span>
          </div>
          <div class="summary-item">
            <span class="label">负责人签字:</span>
            <span class="value signature-line">{{ boundDetail.responsiblePerson || '________________' }}</span>
          </div>
          <div class="summary-item" v-if="boundDetail.signatureDate">
            <span class="label">签字日期:</span>
            <span class="value">{{ formatDate(boundDetail.signatureDate) }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { boundApi } from '@/api/bound'

export default {
  name: 'BoundDetailView',
  props: {
    boundId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      boundDetail: {},
      boundInfoList: []
    }
  },
  computed: {
    // 计算总金额
    totalAmount() {
      return this.boundInfoList.reduce((total, item) => {
        const amount = parseFloat(item.subtotal) || 0
        return total + amount
      }, 0)
    },
    // 金额大写
    amountInWords() {
      return this.convertToChinese(this.totalAmount)
    }
  },
  created() {
    this.loadBoundDetail()
  },
  methods: {
    // 加载入库单详情
    async loadBoundDetail() {
      this.loading = true
      try {
        const response = await boundApi.getBoundById(this.boundId)
        if (response.code === 200) {
          this.boundDetail = response.data || {}
          this.boundInfoList = this.boundDetail.boundInfoList || []
        } else {
          this.$message.error(response.message || '查询失败')
        }
      } catch (error) {
        console.error('查询入库单详情失败:', error)
        this.$message.error('查询失败，请稍后重试')
      } finally {
        this.loading = false
      }
    },

    // 返回
    handleBack() {
      this.$emit('close')
    },

    // 打印
    handlePrint() {
      window.print()
    },

    // 格式化日期
    formatDate(date) {
      if (!date) return '--'
      const d = new Date(date)
      return `${d.getFullYear()}/${String(d.getMonth() + 1).padStart(2, '0')}/${String(d.getDate()).padStart(2, '0')}`
    },

    // 格式化价格
    formatPrice(price) {
      if (!price) return '0.00'
      return parseFloat(price).toFixed(2)
    },

    // 数字转中文大写
    convertToChinese(num) {
      if (num === 0) return '零元'
      
      const digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
      const units = ['', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿']
      
      const numStr = Math.floor(num).toString()
      let result = ''
      
      for (let i = 0; i < numStr.length; i++) {
        const digit = parseInt(numStr[i])
        const unitIndex = numStr.length - i - 1
        
        if (digit !== 0) {
          result += digits[digit] + units[unitIndex]
        } else if (result && !result.endsWith('零')) {
          result += '零'
        }
      }
      
      // 处理小数部分
      const decimal = Math.round((num - Math.floor(num)) * 100)
      if (decimal > 0) {
        const jiao = Math.floor(decimal / 10)
        const fen = decimal % 10
        
        if (jiao > 0) {
          result += digits[jiao] + '角'
        }
        if (fen > 0) {
          result += digits[fen] + '分'
        }
      }
      
      return result + '元'
    }
  }
}
</script>

<style scoped>
.bound-detail-view {
  padding: 20px;
  background-color: #fff;
  font-family: 'Microsoft YaHei', Arial, sans-serif;
  line-height: 1.6;
}

/* 文档标题和操作按钮 */
.document-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 30px;
}

.document-title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin: 0;
  text-align: center;
  flex: 1;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.back-btn {
  background-color: #67c23a;
  border-color: #67c23a;
  color: #fff;
  padding: 8px 16px;
  border-radius: 4px;
  font-size: 14px;
}

.print-btn {
  background-color: #409eff;
  border-color: #409eff;
  color: #fff;
  padding: 8px 16px;
  border-radius: 4px;
  font-size: 14px;
}

/* 基本信息 */
.basic-info {
  margin-bottom: 20px;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 15px;
  background-color: #fafafa;
}

.info-row {
  display: flex;
  gap: 30px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.info-row:last-child {
  margin-bottom: 0;
}

.info-item {
  display: flex;
  align-items: center;
  min-width: 200px;
}

.info-item.full-width {
  min-width: 100%;
}

.info-item .label {
  font-weight: 600;
  color: #333;
  margin-right: 8px;
  min-width: 80px;
}

.info-item .value {
  color: #333;
  flex: 1;
}

/* 表格样式 */
.table-section {
  margin-bottom: 30px;
}

.detail-table {
  width: 100%;
  border-collapse: collapse;
  border: 1px solid #333;
  font-size: 14px;
}

.detail-table th {
  background-color: #f5f5f5;
  border: 1px solid #333;
  padding: 8px 4px;
  text-align: center;
  font-weight: 600;
  color: #333;
}

.detail-table td {
  border: 1px solid #333;
  padding: 8px 4px;
  text-align: center;
  color: #333;
}

.detail-table .number {
  text-align: right;
  padding-right: 8px;
}

.detail-table tbody tr:nth-child(even) {
  background-color: #f9f9f9;
}

/* 汇总和签名区域 */
.summary-section {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-top: 20px;
}

.summary-left {
  flex: 1;
}

.summary-center {
  flex: 1;
  text-align: center;
}

.summary-right {
  flex: 1;
  text-align: right;
}

.summary-item {
  margin-bottom: 15px;
  display: flex;
  align-items: center;
}

.summary-center .summary-item {
  justify-content: center;
}

.summary-right .summary-item {
  justify-content: flex-end;
}

.summary-item .label {
  font-weight: 600;
  color: #333;
  margin-right: 8px;
}

.summary-item .value {
  color: #333;
}

.total-amount {
  font-size: 18px;
  font-weight: bold;
  color: #e6a23c;
}

.signature-line {
  border-bottom: 1px solid #333;
  padding-bottom: 2px;
  min-width: 120px;
  display: inline-block;
}

/* 打印样式 */
@media print {
  .bound-detail-view {
    padding: 0;
    background-color: #fff;
  }
  
  .header-actions {
    display: none;
  }
  
  .document-title {
    font-size: 20px;
  }
  
  .detail-table {
    font-size: 12px;
  }
  
  .detail-table th,
  .detail-table td {
    padding: 4px 2px;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bound-detail-view {
    padding: 15px;
  }
  
  .document-header {
    flex-direction: column;
    align-items: center;
    gap: 15px;
  }
  
  .document-title {
    font-size: 20px;
  }
  
  .info-row {
    flex-direction: column;
    gap: 10px;
  }
  
  .info-item {
    min-width: auto;
  }
  
  .summary-section {
    flex-direction: column;
    gap: 20px;
  }
  
  .summary-center,
  .summary-right {
    text-align: left;
  }
  
  .summary-center .summary-item,
  .summary-right .summary-item {
    justify-content: flex-start;
  }
  
  .detail-table {
    font-size: 12px;
  }
  
  .detail-table th,
  .detail-table td {
    padding: 4px 2px;
  }
}
</style>